package com.zefor.utils;

import org.apache.pulsar.client.api.*;

public class AdmqClient {
    private final static String proxyUrl = "pulsar://123.114.60.34:16339";
    public static PulsarClient client = null;
    private Consumer<byte[]> consumer = null;

    static {
        try {
            client = PulsarClient.builder()
                .serviceUrl(proxyUrl)
                .authentication(AuthenticationFactory.token("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxNjg0NzMzNjA4In0.Kh4Dlb5LibWJIa-hrFF5Y1Rl-fiZapqhp8YAoEPV2nA"))
                .build();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public enum TopicEnum {
        topicEnv("persistent://Y1wuy9mvcD/-CgTvY-Xh/EnvironmentParams-1"),
        topicEle("persistent://Y1wuy9mvcD/-CgTvY-Xh/ElectricityParams-1"),
        topicWater("persistent://Y1wuy9mvcD/-CgTvY-Xh/WaterParams-1");
        public Producer<String> producer;
        TopicEnum(String topicName) {
            try {
                this.producer = client.newProducer(Schema.STRING)
                    .topic(topicName)
                    .create();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    Consumer<byte[]> getConsumer(String topic) throws Exception {
        if (consumer == null) {
            this.consumer = client.newConsumer()
                .topic(topic)
                .subscriptionName(topic)
                .subscribe();
        }
        return consumer;
    }
    
}
